import pymysql
class Mysql:
    def __init__(self,host,user,pwd,db):
        self.conn = pymysql.connect(host=host,user=user,password =pwd,database =db,charset = 'utf8')
        self.cur = self.conn.cursor(pymysql.cursors.DictCursor)
        if self.cur:
            print('连接成功，创建游标对象成功')
        else:
            print('连接失败，创建游标对象失败')

    def insert(self,sql):
        rows = self.cur.execute(sql)
        if rows > 0:
            self.conn.commit()
            print('插入',rows,'条数据成功')
        else:
            print('插入数据失败')

    def select(self,sql):
        rows = self.cur.execute(sql)
        if rows > 0:
            print("查询结果：",self.cur.fetchall())
        else:
            print('未查询到结果')

if __name__ == '__main__':
    mysql = Mysql('39.100.84.234','test','z1p2v0FZdyJPqFBfztIm','test')
    mysql.insert('insert into test(name) value(3)')
    mysql.select('select * from test')